package com.laotou99.wmsjjj.wmsjjj.service;
 

import java.util.Collections;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.laotou99.wmsjjj.wmsjjj.bean.po.SKUPO;
import com.laotou99.wmsjjj.wmsjjj.bean.po.StoreKeyPo;
import com.laotou99.wmsjjj.wmsjjj.constant.C;
import com.laotou99.wmsjjj.wmsjjj.repository.SKUPORepository;
import com.laotou99.wmsjjj.wmsjjj.repository.StoreKeyRepository;


/**
 * @author gjj
 * */

@Service
public class BaseInfoService {

	@Autowired
	private StoreKeyRepository storeKeyRepository;
	
	@Autowired
	private SKUPORepository sKUPORepository;
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	//显示所有客户列表
	@SuppressWarnings("rawtypes")
	public String listStoreKey(JSONObject jo1) throws Exception { 
		
		String result = "";
		List<StoreKeyPo> li2 = storeKeyRepository.findAll();
		System.out.println("li2-=>"+JSON.toJSONString(li2));
		if(li2.isEmpty() || li2.size() == 0) { 
			List li1 = initialStoreKey(); 
			result = JSON.toJSONString(li1);
			System.out.println("1 result-=>"+result);
			return result;
		}
		// 使用 Collections.reverse() 反转列表
        Collections.reverse(li2);
		result = JSON.toJSONString(li2);
		System.out.println("2 result-=>"+result);
		return result;
//		return "{\"aaa\":1234}";
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	private List initialStoreKey() {
		
		List li1 = Lists.newArrayList();
		StoreKeyPo skp1 = null;
		for(int i1=0;i1<10;i1++) {
			skp1 = new StoreKeyPo();
			skp1.setStoreKey("TestKey_"+C.getRnd1());
			skp1.setStoreName("测试客户名"+C.getRnd2());
			skp1.setStoreNO("No."+C.getRnd1());
			skp1.setAddress("测试地址"+C.getRnd1());
			storeKeyRepository.save(skp1);
			skp1.setId(i1+1);
			li1.add(skp1);
		}
		
		return li1;
	}
	
	public String detailStoreKey(JSONObject jo1) throws Exception { 

		String result = "";
		StoreKeyPo skp1 = new StoreKeyPo();
		skp1.setId(0+1);
		skp1.setStoreKey("TestKey_"+C.getRnd1());
		skp1.setStoreName("测试客户名"+C.getRnd2());
		skp1.setStoreNO("No."+C.getRnd1());
		skp1.setAddress("测试地址"+C.getRnd1());

		result = JSON.toJSONString(skp1);
		System.out.println("result-=>"+result);
		return result; 
	}
	
	public String listSKU(JSONObject jo1) throws Exception { 

		String result = "";
		List<SKUPO> findAll = sKUPORepository.findAll();
		System.out.println("findAll-=>"+JSON.toJSONString(findAll));

        Collections.reverse(findAll);
		result = JSON.toJSONString(findAll);
		System.out.println("2 result-=>"+result); 
		
		return result;
	}

	public void test1() {
		System.out.println("test1()");

		List<StoreKeyPo> li2 = storeKeyRepository.findAll();
		System.out.println("li2-=>"+JSON.toJSONString(li2));
		
		StoreKeyPo skp1 = new StoreKeyPo(); 
		skp1.setStoreKey("TestKey_"+C.getRnd1());
		skp1.setStoreName("测试客户名"+C.getRnd2());
		skp1.setStoreNO("No."+C.getRnd1());
		skp1.setAddress("测试地址"+C.getRnd1());
		System.out.println("skp1-=>"+JSON.toJSONString(skp1));
		StoreKeyPo skp2 = storeKeyRepository.save(skp1);
		System.out.println("skp2-=>"+JSON.toJSONString(skp2));
 
	}

	public void test2() {
		System.out.println("test2()"); 
		
		List<StoreKeyPo> li1 = Lists.newArrayList();
		for(int i1=0;i1<10;i1++) {
			StoreKeyPo skp1 = new StoreKeyPo();
			skp1.setStoreKey("TestKey_"+C.getRnd1());
			skp1.setStoreName("测试客户名"+C.getRnd2());
			skp1.setStoreNO("No."+C.getRnd1());
			skp1.setAddress("测试地址"+C.getRnd1());  
			li1.add(skp1);
		} 
		System.out.println("li1-=>"+JSON.toJSONString(li1));
		Iterator<StoreKeyPo> it1 = li1.iterator();
		ListIterator<StoreKeyPo> lit1 = li1.listIterator(); 
		Iterable<StoreKeyPo> it2 = new Iterable<StoreKeyPo>() { 
			@Override
			public Iterator<StoreKeyPo> iterator() { 
				return it1;
			}
		};
		List<StoreKeyPo> li2 = storeKeyRepository.save(it2);
		System.out.println("li2-=>"+JSON.toJSONString(li2));
		
		
	}

	public void test3() {
		System.out.println("test3()");
		
		List<SKUPO> li1 = sKUPORepository.findAll();
		System.out.println("li1-=>"+JSON.toJSONString(li1));
		
		List<Map<String, Object>> queryForList1 = 
				jdbcTemplate.queryForList("SELECT * FROM STORE_KEY_PO ");
		System.out.println("queryForList1-=>"+JSON.toJSONString(queryForList1));
		
	}

	public String addNew(JSONObject jo1) throws Exception  {
		System.out.println("jo1-=>"+jo1.toJSONString());

		StoreKeyPo skp1 = new StoreKeyPo();
		StoreKeyPo skp2 = jo1.parseObject(jo1.toJSONString(), StoreKeyPo.class);
		skp2.setAddDate(new Date());
		StoreKeyPo skp3 = storeKeyRepository.save(skp2);
		System.out.println("skp2-=>"+JSON.toJSONString(skp3));
		
		return "OK";
	}

	public String update(JSONObject jo1) throws Exception  {
		System.out.println("update jo1-=>"+jo1.toJSONString());		

		StoreKeyPo skp2 = jo1.parseObject(jo1.toJSONString(), StoreKeyPo.class);
		StoreKeyPo skp3 = storeKeyRepository.save(skp2);
		System.out.println("skp2-=>"+JSON.toJSONString(skp3));
		
		return "OK";
	}

	public String selectById(JSONObject jo1) throws Exception {
		
		Long lng1 = jo1.getLong("id");
		StoreKeyPo findOne = storeKeyRepository.findOne(lng1);
		
		return JSON.toJSONString(findOne);
	}

	public String delete(JSONObject jo1) throws Exception {
		
		Long id = jo1.getLong("id");
		storeKeyRepository.delete(id);
		
		return "OK";
	}

	public String addNewSKU(JSONObject jo1) throws Exception {
		System.out.println("jo1-=>"+jo1.toJSONString());
		
		SKUPO po1 = new SKUPO();
		SKUPO po2 = jo1.parseObject(jo1.toJSONString(), SKUPO.class);
		po2.setCreateDate(new Date());
		SKUPO po3 = sKUPORepository.save(po2);
		System.out.println("po3-=>"+JSON.toJSONString(po3));
		
		return "OK";
	}

	public String updateSKU(JSONObject jo1) throws Exception {
		System.out.println("updateSKU jo1-=>"+jo1.toJSONString());		

		SKUPO sp1 = jo1.parseObject(jo1.toJSONString(), SKUPO.class);
		SKUPO sp2 = sKUPORepository.save(sp1);
		System.out.println("sp2-=>"+JSON.toJSONString(sp2)); 
		
		return "OK";
	}

	public String deleteSKU(JSONObject jo1) throws Exception { 
		System.out.println("deleteSKU jo1-=>"+jo1.toJSONString());	

		Long id = jo1.getLong("id");
		sKUPORepository.delete(id);

		return "OK";
	}

	public String selectSKUById(JSONObject jo1) throws Exception { 
		System.out.println("selectSKUById jo1-=>"+jo1.toJSONString());	 

		Long lng1 = jo1.getLong("id");
		SKUPO findOne = sKUPORepository.findOne(lng1); 
		
		return JSON.toJSONString(findOne);
	}
}
